package com.mediatek.datatransfer;

import android.app.ActionBar;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.DialogInterface.OnShowListener;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.text.format.Formatter;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.WindowManager.BadTokenException;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mediatek.datatransfer.CheckedListActivity.OnCheckedCountChangedListener;
import com.mediatek.datatransfer.RestoreService.OnRestoreStatusListener;
import com.mediatek.datatransfer.RestoreService.RestoreBinder;
import com.mediatek.datatransfer.ResultDialog.ResultEntity;
import com.mediatek.datatransfer.SDCardReceiver.OnSDCardStatusChangedListener;
import com.mediatek.datatransfer.modules.Composer;
import com.mediatek.datatransfer.utils.Constants.DialogID;
import com.mediatek.datatransfer.utils.Constants.MessageID;
import com.mediatek.datatransfer.utils.Constants.State;
import com.mediatek.datatransfer.utils.ModuleType;
import com.mediatek.datatransfer.utils.MyLogger;
import com.mediatek.datatransfer.utils.SDCardUtils;
import com.mediatek.datatransfer.utils.ScreenLock;
import com.mediatek.datatransfer.utils.Utils;
import com.mediatek.datatransfer.utils.NotifyManager;

import java.io.IOException;
import java.util.ArrayList;

/**
 * @author mtk81330
 *
 */
public abstract class AbstractRestoreActivity extends CheckedListActivity implements
        OnCheckedCountChangedListener {

    private static final String CLASS_TAG = MyLogger.LOG_TAG + "/AbstractRestoreActivity";
    protected ArrayList<String> mUnCheckedList = new ArrayList<String>();
    protected Handler mHandler;
    BaseAdapter mAdapter;
    protected ImageButton mBtRestore = null;
    private CheckBox mChboxSelect = null;
    private TextView mCheckBoxSelectHint;
    private View mDivider ;
    private ProgressDialog mProgressDialog;
    private int mSelectedRestorePath;
    protected RestoreBinder mRestoreService;
    //A NBHSWL-399 by huan.yang 20160505 @{
    protected ProgressDialog mCancelDlg;
    protected long mCancelDlgShowTime;
    protected long mCancelDlgDismissTime;
    private AlertDialog mComfirmCancelDialog;
    //@}
    protected final int mCancelDlgExistTime = 800;
    OnRestoreStatusListener mRestoreListener;
    OnSDCardStatusChangedListener mSDCardListener;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        super.onCreate(savedInstanceState);
        setProgressBarIndeterminateVisibility(false);
        setContentView(R.layout.restore);
        //ActionBar actionBar = getActionBar();
        //actionBar.setDisplayHomeAsUpEnabled(true);
        init();

        /*
         * bind Restore Service when activity onCreate, and unBind Service when
         * activity onDestroy
         */
        this.bindService();
        
        addEmptyFooterView();
        MyLogger.logI(CLASS_TAG, " onCreate");
    }

    public void addEmptyFooterView(){
    	View view = getLayoutInflater().inflate(R.layout.tecno_list_footer_view, null, false);
        getListView().addFooterView(view , null , false);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            // app icon in action bar clicked; go home
            onBackPressed();
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyLogger.logI(CLASS_TAG, " onDestroy");
        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }
        unRegisteSDCardListener();

        // when startService when to Restore and stopService when onDestroy if
        // the service in IDLE
        if (mRestoreService != null && mRestoreService.getState() == State.INIT) {
            this.stopService();
        }

        // set listener to null avoid some special case when restart after
        // configure changed
        if (mRestoreService != null) {
            mRestoreService.setOnRestoreChangedListner(null);
        }
        this.unBindService();
        mHandler = null;
        ScreenLock.instance().releaseWakeLock();//A by huan.yang 20151217
    }

    @Override
    public void onPause() {
        super.onPause();
        MyLogger.logI(CLASS_TAG, " onPasue");
    }

    @Override
    public void onStop() {
        super.onStop();
        MyLogger.logI(CLASS_TAG, " onStop");
        if (mConfirmDialog != null && mConfirmDialog.isShowing()) {
            mConfirmDialog.dismiss();
        }
    }

    @Override
    protected void onStart() {
        MyLogger.logI(CLASS_TAG, " onStart");
        super.onStart();
    }

    /**
     * onConfigurationChanged.
     */
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        MyLogger.logI(CLASS_TAG, " onConfigurationChanged");
    }

    @Override
    public void onCheckedCountChanged() {
        mAdapter.notifyDataSetChanged();
        updateButtonState();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void init() {
    	initActionBar();
        registerOnCheckedCountChangedListener(this);
        registerSDCardListener();
        initButton();
        initHandler();
        initLoadingView();
        initDetailList();
        createProgressDlg();
    }

    private void initActionBar(){
    	LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View customActionBarView = inflater.inflate(R.layout.tecno_multiple_choice_custom_action_bar, null);
        final CheckBox selectBox = (CheckBox) customActionBarView.findViewById(R.id.all_select_box);
        View back = customActionBarView.findViewById(R.id.action_back_ll);
        back.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setResult(RESULT_CANCELED);
                finish();
            }
        }); 
        ActionBar bar = this.getActionBar();
        if(bar != null){
        	bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM,
                    ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME
                            | ActionBar.DISPLAY_SHOW_TITLE);
        	bar.setCustomView(customActionBarView);
        }
    }
    
    LinearLayout mLoadingContent = null;

    private void initLoadingView() {
        mLoadingContent = (LinearLayout) findViewById(R.id.loading_container);
    }

    protected void showLoadingContent(boolean show) {
        findViewById(R.id.loading_container).setVisibility(show ? View.VISIBLE : View.GONE);
        findViewById(R.id.restore_content).setVisibility(!show ? View.VISIBLE : View.GONE);
    }

    protected Dialog onCreateDialog(int id) {
        return onCreateDialog(id, null);
    }

    Dialog mConfirmDialog = null;
    protected Dialog onCreateDialog(int id, Bundle args) {
        Dialog dialog = null;
        MyLogger.logI(CLASS_TAG, " oncreateDialog, id = " + id);
        switch (id) {

        case DialogID.DLG_RESTORE_CONFIRM:
            mConfirmDialog = dialog = new AlertDialog.Builder(this)
                    .setTitle(R.string.notice).setMessage(R.string.restore_confirm_notice)
                    .setNegativeButton(android.R.string.cancel, null)
                    .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            MyLogger.logI(CLASS_TAG, "start to Restore");
                            startRestore();
                        }
                    }).setCancelable(true).create();
            break;

        case DialogID.DLG_SDCARD_REMOVED:
            dialog = new AlertDialog.Builder(this).setTitle(R.string.error)
                    .setMessage(R.string.sdcard_removed)
                    .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int which) {
                            if (mRestoreService != null) {
                                mRestoreService.reset();
                            }
                            stopService();
                        }
                    }).setCancelable(false).create();
            break;
        case DialogID.DLG_RUNNING:
            dialog = new AlertDialog.Builder(AbstractRestoreActivity.this)
                    .setTitle(R.string.warning)
                    .setMessage(R.string.state_running)
                    .setPositiveButton(android.R.string.ok, null).create();
            break;
        case DialogID.DLG_SDCARD_FULL:
            dialog = new AlertDialog.Builder(this).setTitle(R.string.error)
                    .setMessage(R.string.sdcard_is_full)
                    .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

                        public void onClick(DialogInterface dialog, int which) {
                            dismissProgressDialog();
                            if (mRestoreService != null) {
                                mRestoreService.cancelRestore();
                            }
							if (mRestoreService != null) {
                                mRestoreService.reset();
                            }
                            stopService();
                            NotifyManager.getInstance(AbstractRestoreActivity.this)
                                    .clearNotification();
                        }
                    }).setCancelable(false).create();
            break;
        case DialogID.DLG_SDCARD_MS_LACK:
            dialog = new AlertDialog.Builder(this).setTitle(R.string.error)
                    //M WSHBLL-620 by huan.yang 20160318 @{
                    .setMessage(R.string.insufficient_memory).setPositiveButton(android.R.string.ok,/*null*/new DialogInterface.OnClickListener() {
                        
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub
                            stopService();
                            AbstractRestoreActivity.this.finish();
                        }
                    })
                    //@}
                    .setCancelable(true).create();
            break;
        case DialogID.DLG_CANCEL_CONFIRM:
            dialog = new AlertDialog.Builder(AbstractRestoreActivity.this)
                    .setTitle(R.string.warning).setMessage(R.string.cancel_restore_confirm)
                    .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(final DialogInterface arg0, final int arg1) {
                            if (mRestoreService != null && mRestoreService.getState() != State.INIT
                                    && mRestoreService.getState() != State.FINISH) {
                                if (mCancelDlg == null) {
                                    mCancelDlg = createCancelDlg();
                                }
                                mCancelDlgShowTime = System.currentTimeMillis();
                                mCancelDlg.show();
                                mRestoreService.cancelRestore();
                            }
                        }
                    }).setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
                        public void onClick(final DialogInterface arg0, final int arg1) {

                            if (mRestoreService != null && mRestoreService.getState() == State.PAUSE) {
                                mRestoreService.continueRestore();
                            }
                            if (mProgressDialog != null) {
                                mProgressDialog.show();
                            }
                        }
                    }).setCancelable(false).create();
          //A HIOSWY-4327 by huan.yang 20151204 @{
            mComfirmCancelDialog = (AlertDialog) dialog;
            if(mComfirmCancelDialog!=null){
            mComfirmCancelDialog.setOnShowListener(new OnShowListener() {
                
                @Override
                public void onShow(DialogInterface dialog) {
                    // TODO Auto-generated method stub
                    ((Button)mComfirmCancelDialog.getWindow().findViewById(android.R.id.button1)).setTextColor(0xffff0000);
                }
            });
            }
           // @}
            break;
        case DialogID.DLG_SELECT_RESTORE_PATH:
          //A CJHWSW-1277 by huan.yang 20160324@{
            String externalStoragePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath();
           //@}
            dialog = new AlertDialog.Builder(this).setTitle(R.string.media_file_restore_to)
                     .setNegativeButton(android.R.string.cancel, null)
                     .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                        
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // TODO Auto-generated method stub
                            startRestore(mSelectedRestorePath);
                        }
                    })
                    //M CJHWSW-1277 by huan.yang 20160324@{
                     .setSingleChoiceItems(new String[]{getResources().getString(R.string.sdcard)+"  "+getResources().getString(R.string.free_space,Formatter.formatFileSize(this, SDCardUtils.getAvailableSize(SDCardUtils.getExternalStoragePath(this)))),
                             getResources().getString(R.string.phone_storage)+"  "+getResources().getString(R.string.free_space,Formatter.formatFileSize(this, SDCardUtils.getAvailableSize(externalStoragePath)))}, 
                             0, new DialogInterface.OnClickListener() {
                     //@}
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            mSelectedRestorePath = which;
                        }
                    }).create();
        default:
            break;
        }
        return dialog;
    }
    //A NBHSWL-399 by huan.yang 20160505 @{ 
    private ProgressDialog createCancelDlg() {
        if (mCancelDlg == null) {
            mCancelDlg = new ProgressDialog(this);            
            mCancelDlg.setMessage(getString(R.string.cancelling));
            mCancelDlg.setCancelable(false);
        }
        return mCancelDlg;
    }
    //@}
    private void unRegisteSDCardListener() {
        if (mSDCardListener != null) {
            SDCardReceiver receiver = SDCardReceiver.getInstance();
            receiver.unRegisterOnSDCardChangedListener(mSDCardListener);
        }
    }

    private void registerSDCardListener() {
        mSDCardListener = new OnSDCardStatusChangedListener() {
            @Override
            public void onSDCardStatusChanged(boolean mount) {
                if (!mount) {

                    AbstractRestoreActivity.this.finish();
                }
            }
        };

        SDCardReceiver receiver = SDCardReceiver.getInstance();
        receiver.registerOnSDCardChangedListener(mSDCardListener);
    }

    private void initHandler() {
        mHandler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                // TODO Auto-generated method stub
                switch (msg.what) {
                    case MessageID.PRESS_BACK:
                        if (mRestoreService != null && mRestoreService.getState() != State.INIT
                                && mRestoreService.getState() != State.FINISH) {
                            mRestoreService.pauseRestore();
                            AbstractRestoreActivity.this.showDialog(DialogID.DLG_CANCEL_CONFIRM);
                        }
                        break;
                }
            }
        };
    }

    private void initButton() {
        mDivider = findViewById(R.id.restore_divider);
        mDivider.setBackground(getListView().getDivider());
        mBtRestore = (ImageButton)findViewById(R.id.backup_bt_backcup);
        //mBtRestore.setOutlineProvider(MainActivity.OVAL_OUTLINE_PROVIDER);
        mBtRestore.setBackground(getDrawable(R.drawable.ic_ellipse_yellow_bg));
        mBtRestore.setImageResource(R.drawable.ic_recovery);
        //HIOSWY-5384 by huan.yang 20151218 @{
        if(getCheckedCount()==0){
        	mBtRestore.setAlpha(0.7f);
        }
        //@}
        mBtRestore.setOnClickListener(new Button.OnClickListener() {

            public void onClick(View v) {
                if (getCheckedCount() > 0) {
                    if (Utils.getWorkingInfo() > 0) {
                        showDialog(DialogID.DLG_RUNNING);
                        return;
                    }
                    if(AbstractRestoreActivity.this instanceof PersonalDataRestoreActivity && selectedContainsMedia()){
                        mSelectedRestorePath = 0;
                        showDialog(DialogID.DLG_SELECT_RESTORE_PATH);  
                    }else{
                        showDialog(DialogID.DLG_RESTORE_CONFIRM);
                    }
                }
            }
        });
        mCheckBoxSelectHint = (TextView)findViewById(R.id.select_items);
        mCheckBoxSelectHint.setText("");
        //mChboxSelect = (CheckBox) findViewById(R.id.restore_checkbox_select);
        mChboxSelect = (CheckBox) findViewById(R.id.all_select_box);
        mChboxSelect.setVisibility(View.INVISIBLE);
        mChboxSelect.setOnClickListener(new CheckBox.OnClickListener() {

            public void onClick(View v) {
                if (isAllChecked(true)) {
                    setAllChecked(false);
                } else {
                    setAllChecked(true);
                }
            }
        });
    }

    protected void updateButtonState() {
        if (getCount() == 0) {
            setButtonsEnable(false);
            mChboxSelect.setVisibility(View.GONE);
            return;
        }
        mChboxSelect.setVisibility(View.VISIBLE);
        //mChboxSelect.setText(getApplication().getResources().getString(R.string.selectall));
        //mDivider.setVisibility(View.VISIBLE);
        if (isAllChecked(false)) {
            mBtRestore.setEnabled(false);
            mBtRestore.setAlpha(0.7f);//A HIOSWY-5384 by huan.yang 20151218
            mChboxSelect.setChecked(false);
        } else {
            mBtRestore.setEnabled(true);
            mBtRestore.setAlpha(1f);//A HIOSWY-5384 by huan.yang 20151218
            mChboxSelect.setChecked(isAllChecked(true));
        }
    }

    protected void setButtonsEnable(boolean enabled) {
        if (mChboxSelect != null) {
            mChboxSelect.setEnabled(enabled);
        }
        if (mBtRestore != null) {
            mBtRestore.setEnabled(enabled);
        }
    }

    private void initDetailList() {
        mAdapter = initAdapter();
        setListAdapter(mAdapter);
        this.updateButtonState();
    }
    private boolean selectedContainsMedia() {
        int count = getCount();
        for (int position = 0; position < count; position++) {
            PersonalItemData item = (PersonalItemData) getItemByPosition(position);
            if (isItemCheckedByPosition(position)) {
                if(item.getType()==ModuleType.TYPE_MUSIC||item.getType()==ModuleType.TYPE_PICTURE){
                    return true;        
                }             
            }
        }
        return false;
    }    
    abstract protected BaseAdapter initAdapter();

    protected void notifyListItemCheckedChanged() {
        if (mAdapter != null) {
            mAdapter.notifyDataSetChanged();
        }
        updateButtonState();
    }

    protected ProgressDialog createProgressDlg() {
        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(this);
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            mProgressDialog.setMessage(getString(R.string.restoring));
            mProgressDialog.setCancelable(true);//M by huan.yang 20160505 //false to true
            mProgressDialog.setCanceledOnTouchOutside(false);//A by huan.yang 20160505
            mProgressDialog.setCancelMessage(mHandler.obtainMessage(MessageID.PRESS_BACK));//A by huan.yang 20160505
        }
        return mProgressDialog;
    }

    protected void showProgressDialog() {
        if (mProgressDialog == null) {
            mProgressDialog = createProgressDlg();
        }
        if (this != null && !this.isFinishing()) {
            try {
                mProgressDialog.show();
            } catch (BadTokenException e) {
                MyLogger.logE(CLASS_TAG, " BadTokenException :" + e.toString());
            }
        }
    }

    protected void setProgressDialogMax(int max) {
        if (mProgressDialog == null) {
            mProgressDialog = createProgressDlg();
        }
        mProgressDialog.setMax(max);
    }

    protected void setProgressDialogProgress(int value) {
        if (mProgressDialog == null) {
            mProgressDialog = createProgressDlg();
        }
        mProgressDialog.setProgress(value);
    }

    protected void setProgressDialogMessage(CharSequence message) {
        if (mProgressDialog == null) {
            mProgressDialog = createProgressDlg();
        }
        mProgressDialog.setMessage(message);
    }

    protected boolean isProgressDialogShowing() {
        return mProgressDialog.isShowing();
    }

    protected void dismissProgressDialog() {
        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }
    }

    protected boolean isCanStartRestore() {
        if (mRestoreService == null) {
            MyLogger.logE(CLASS_TAG, " isCanStartRestore : mRestoreService is null");
            return false;
        }

        if (mRestoreService.getState() != State.INIT) {
            MyLogger.logE(CLASS_TAG,
                    " isCanStartRestore :Can not to start Restore. Restore Service is ruuning");
            return false;
        }
        return true;
    }

    protected boolean errChecked() {
        boolean ret = false;
        String path = SDCardUtils.getStoragePath(this);
        if (path == null) {
            // no sdcard
            ret = true;
            if (mHandler != null) {
                mHandler.post(new Runnable() {
                    public void run() {
                        showDialog(DialogID.DLG_SDCARD_REMOVED, null);
                    }
                });
            }
        } else if (SDCardUtils.getAvailableSize(path) <= 512) {
            // no space
            ret = true;
            if (mHandler != null) {
                mHandler.post(new Runnable() {
                    public void run() {
                        showDialog(DialogID.DLG_SDCARD_FULL, null);
                    }
                });
            }
        }
        return ret;
    }

    /**
     * @param listener.
     */
    public void setOnRestoreStatusListener(OnRestoreStatusListener listener) {
        mRestoreListener = listener;
        if (mRestoreService != null) {
            mRestoreService.setOnRestoreChangedListner(mRestoreListener);
        }
    }

    protected abstract void afterServiceConnected();

    protected abstract void startRestore(int restorePlace);
    protected abstract void startRestore();

    private void bindService() {
        getApplicationContext().bindService(new Intent(this, RestoreService.class), mServiceCon,
                Service.BIND_AUTO_CREATE);
    }

    private void unBindService() {
        if (mRestoreService != null) {
            mRestoreService.setOnRestoreChangedListner(null);
        }
        getApplicationContext().unbindService(mServiceCon);
    }

    protected void startService() {
        startService(new Intent(this, RestoreService.class));
    }

    protected void stopService() {
        if (mRestoreService != null) {
            mRestoreService.reset();
        }
        stopService(new Intent(this, RestoreService.class));
    }

    ServiceConnection mServiceCon = new ServiceConnection() {

        public void onServiceConnected(ComponentName name, IBinder service) {
            mRestoreService = (RestoreBinder) service;
            if (mRestoreService != null) {
                mRestoreService.setOnRestoreChangedListner(mRestoreListener);
                afterServiceConnected();
            }
            MyLogger.logI(CLASS_TAG, " onServiceConnected");
        }

        public void onServiceDisconnected(ComponentName name) {
            mRestoreService = null;
            MyLogger.logI(CLASS_TAG, " onServiceDisconnected");
        }
    };

    /**
     * @author mtk81330
     *
     */
    public class NormalRestoreStatusListener implements OnRestoreStatusListener {

        /**
         * @param type type
         * @param max
         */
        public void onComposerChanged(final int type, final int max) {
            MyLogger.logI(CLASS_TAG, "onComposerChanged");
        }

        /**
         * @param composer.
         * @param progress.
         */
        public void onProgressChanged(Composer composer, final int progress) {
            MyLogger.logI(CLASS_TAG, "onProgressChange, p = " + progress);
            if (mHandler != null) {
                mHandler.post(new Runnable() {
                    public void run() {
                        if (mProgressDialog != null) {
                            mProgressDialog.setProgress(progress);
                        }
                    }
                });
            }
        }

        /**
         * @param bSuccess if success
         * @param resultRecord result
         */
        public void onRestoreEnd(boolean bSuccess, final ArrayList<ResultEntity> resultRecord) {
            MyLogger.logI(CLASS_TAG, "onRestoreEnd");
        }

        /**
         * onRestoreErr.
         */
        public void onRestoreErr(IOException e) {
            MyLogger.logI(CLASS_TAG, "onRestoreErr");
            if (errChecked()) {
                if (mRestoreService != null && mRestoreService.getState() != State.INIT
                        && mRestoreService.getState() != State.FINISH) {
                    mRestoreService.pauseRestore();
                }
            }
        }
    }

    @Override
    public void onBackPressed() {
        if (mRestoreService != null && mRestoreService.getState() == State.RUNNING) {
            return;
        }
        super.onBackPressed();
    }

    @Override
    public void setTitle(CharSequence title) {
        if(mCheckBoxSelectHint != null){
        	mCheckBoxSelectHint.setText(title);
        }
    }
}
